package logic

import (
	"GuidingSystem/dao"
	"GuidingSystem/errcodes"
	"GuidingSystem/global"
	"GuidingSystem/middlewares"
	"GuidingSystem/model/reply"
	"GuidingSystem/model/request"
	"errors"
	"github.com/XYYSWK/Rutils/pkg/app/errcode"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"strconv"
	"strings"
	"time"
)

type comment struct{}

func (comment) CreateComment(ctx *gin.Context, userID int64, params *request.Comment) (*reply.Comment, errcode.Err) {
	commentInfo := reply.Comment{
		ID:          0,
		UserID:      userID,
		Content:     params.Content,
		IsReply:     params.IsReply,
		ToCommentID: params.ToCommentID,
		CreateAt:    time.Now(),
		CemeteryID:  params.CemeteryID,
	}
	var data reply.Comment
	var err error
	if commentInfo.IsReply {
		data, err = dao.DB.GetCommentByID(commentInfo.ToCommentID)
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return nil, errcodes.CommentNotExist
			}
			global.Logger.Error(err.Error(), middlewares.ErrLogMsg(ctx)...)
			return nil, errcode.ErrServer
		}
	}
	res, err := dao.DB.CreateComment(commentInfo)
	if err != nil {
		global.Logger.Error(err.Error(), middlewares.ErrLogMsg(ctx)...)
		return nil, errcode.ErrServer
	}
	if commentInfo.IsReply {
		// TODO: 修改被回复的评论的信息
		if data.ReplyCommentIDs == "" {
			data.ReplyCommentIDs = strconv.FormatInt(res.ID, 10)
		} else {
			data.ReplyCommentIDs = data.ReplyCommentIDs + "," + strconv.FormatInt(res.ID, 10)
		}
		err = dao.DB.UpdateComment(&data)
		if err != nil {
			global.Logger.Error(err.Error(), middlewares.ErrLogMsg(ctx)...)
			return nil, errcode.ErrServer
		}
	}
	return &res, nil
}

func (comment) GetCommentsByCemeteryID(ctx *gin.Context, cemeteryID int64) ([]*reply.GetCommentsByCemeteryID, errcode.Err) {
	data, err := dao.DB.GetCommentsByCemeteryID(cemeteryID)
	if err != nil {
		global.Logger.Error(err.Error(), middlewares.ErrLogMsg(ctx)...)
		return nil, errcode.ErrServer
	}
	res := make([]*reply.GetCommentsByCemeteryID, 0, len(data))
	for i, _ := range data {
		replyComments := make([]reply.Comment, 0)
		if len(data[i].ReplyCommentIDs) > 0 {
			replyCommentIDs := strings.Split(data[i].ReplyCommentIDs, ",")
			for _, idStr := range replyCommentIDs {
				var id int64
				if id, err = strconv.ParseInt(idStr, 10, 64); err != nil {
					global.Logger.Error(err.Error(), middlewares.ErrLogMsg(ctx)...)
					return nil, errcode.ErrServer
				}
				replyComment, err := dao.DB.GetCommentByID(id)
				if err != nil {
					global.Logger.Error(err.Error(), middlewares.ErrLogMsg(ctx)...)
					return nil, errcode.ErrServer
				}
				replyComments = append(replyComments, replyComment)
			}
		}
		res = append(res, &reply.GetCommentsByCemeteryID{
			Comment: reply.Comment{
				ID:              data[i].ID,
				UserID:          data[i].UserID,
				Content:         data[i].Content,
				IsReply:         data[i].IsReply,
				ToCommentID:     data[i].ToCommentID,
				CreateAt:        data[i].CreateAt,
				ReplyCommentIDs: data[i].ReplyCommentIDs,
				CemeteryID:      data[i].CemeteryID,
			},
			ReplyComments: replyComments,
		})
	}
	return res, nil
}
